<?php

namespace App\Repositories\Article;

use App\Models\Article\Article;
use App\Models\Article\Category;
use App\Models\Article\Comment;
use App\Models\Article\CommentAnswer;
use App\Models\Article\ArticleTag;
use App\Repositories\Repository;
use App\Services\CategoryService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


/**
 *
 */
class ArticleRepository extends Repository
{
    private $redisKey = "";
    private $catService = "";

    public function __construct()
    {
        $this->redisKey = config('rediskey.article.category');
        $this->catService = new CategoryService();
    }

    public function list($cat_id = '', $page = 1, $pageSzie = 18)
    {
        $articles = Article::with(["comment" => function ($query) {
            $query->with(["answers" => function ($sql) {
                $sql->orderBy("created_at", "desc");
            }])->orderBy("created_at", "desc");
        }, "user"])->when($cat_id, function ($query) use ($cat_id) {
            $query->where('cat_id', $cat_id);
        })->paginate($pageSzie, ['*'], 'page', $page);

        $categorys = Redis::get($this->redisKey);
        if (!$categorys) {
            $categorys = Category::get()->toArray();
            if ($categorys) {
                $categorys = $this->catService->getTree($categorys);
                $categorys = $this->catService->setArrayKey($categorys);
                $categorys = json_encode($categorys);
                Redis::set($this->redisKey, $categorys);
            }
        }
        $categorys = json_decode($categorys, true);
        foreach ($articles as $k => $article) {
            $articles[$k]->cat_id = $categorys[$article->cat_id]['name'];
            if ($article->comment && $article->comment[0]->answers) {
                echo 12;
            }
        }
        return $articles;
    }

    public function show($id)
    {
        $article = Article::with(["comment", "user"])->find($id);
        if ($article) {
            $categorys = Redis::get($this->redisKey);
            if ($categorys) {
                $categorys = json_decode($categorys, true);
                $category = isset($categorys[$article->cat_id]) ? $categorys[$article->cat_id] : "";
            } else {
                $category = Category::where("id", "=", $article->cat_id)->first();
            }
            $article->cat_id = $category;
            if ($article->tags) {
                $tags = explode(",", $article->tags);
                $article->tags = ArticleTag::whereIn("id", $tags)->orderBy("weight", "desc")->get();
            }
        }


        return $article;
    }

    public function store($data)
    {
        try {
            Article::create($data);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            //dd($e->getMessage());
            return $this->respondError(10108);
        }
    }

    public function save($data)
    {
        $article = Article::find($data['id']);
        if ($article) {
            try {
                $article->fill($data)->save();
                return $this->respondSuccess();
            } catch (\Exception $e) {
                return $this->respondError(10106);
            }
        }
        return $this->respondError(10106);
    }

    public function del($id)
    {
        DB::beginTransaction();
        try {
            Article::where("id", "=", $id)->delete();
            Comment::where("article_id", "=", $id)->delete();
            CommentAnswer::where("article_id", "=", $id)->delete();
            DB::commit();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->respondError(10107);
        }
    }


    /*
     * ==========================================================================================
     *                                    私有方法
     * ==========================================================================================
    */

}

